# Schniepp Lab, 2018-2021

# Vector class to handle vector related calculations
import numpy as np

class Vector:
    # __init__ to initilize the class, either from two points (p1 and p2) or from the coordinates
    def __init__(self, p1 = None, p2 = None, x = None, y = None, z = None):
        if p1 != None and p2 != None:
            self.x = p2[0] - p1[0]
            self.y = p2[1] - p1[1]
            self.z = p2[2] - p1[2]
        
        elif x != None and y != None and z != None:
            self.x = x
            self.y = y
            self.z = z
        
        # vec is the vector itself; dimension is the dimension of the vector; norm is the length of the vector
        self.vec = (self.x, self.y, self.z)
        self.dimension = len(self.vec)
        self.norm = np.linalg.norm(self.vec)
    
    # get the dot product between self and v2
    def dot_product(self, v2):
        dot_p = 0
        if v2.dimension != self.dimension:
            print('Wrong vector dimension!')
            return
        
        for i in range(self.dimension):
            dot_p += self.vec[i] * v2.vec[i]
        
        return dot_p
    
    # calculate the angle between vectors self and v2
    def get_angle_between(self, v2, unit = None):
        angle = np.arccos(self.dot_product(v2) / (self.norm * v2.norm))
    
        if unit == 'degree':
            angle = 180 * angle / np.pi
    
        return angle